package sqlist;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestDemo2 {

    public static void main(String[] args) {
        //不带参数的构造方法
        //当调用这个构造方法时，数组的默认大小是0
        //第一次add的时候，默认size才变成10
        //grow函数是扩容函数，扩容方式是1.5倍扩容
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        System.out.println(arrayList);

        //给定容量的构造方法
        ArrayList<Integer> arrayList2 = new ArrayList<>(20);
        arrayList2.add(10);
        arrayList2.addAll(arrayList);
        System.out.println(arrayList2);
        arrayList2.remove(new Integer(10));//得删除对象
        arrayList.indexOf(new Integer(4));//返回下标

        //使用已有的collection
        ArrayList<Integer> arrayList1 = new ArrayList<>(arrayList);
        System.out.println(arrayList1);

        //subList
        List<Integer> list = arrayList.subList(1,3);//截取函数

        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(arrayList.get(i)+" ");
        }
        System.out.println();
        for (Integer x:arrayList) {
            System.out.print(x+" ");
        }

        System.out.println();

        Iterator<Integer> it = arrayList.iterator();
        Iterator<Integer> it2 = arrayList.listIterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");//访问下个，同时it向下走一步
        }
    }
}
